package com.zzyl.nursing.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.bean.BeanUtil;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.exception.ServiceException;
import com.zzyl.nursing.dto.DeviceDto;
import com.zzyl.nursing.vo.DeviceDetailVo;
import com.zzyl.nursing.vo.ProductVo;
import io.swagger.annotations.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.service.IDeviceService;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.common.core.page.TableDataInfo;

/**
 * 设备Controller
 * 
 * @author zhangyu
 * @date 2025-05-30
 */
@RestController
@RequestMapping("/nursing/device")
@Api(tags = "设备相关接口")
public class DeviceController extends BaseController
{
    @Autowired
    private IDeviceService deviceService;

    /**
     * 查询设备列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/list")
    @ApiOperation(value = "获取设备列表")
    public TableDataInfo<List<Device>> list(Device device)
    {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }

    /**
     * 导出设备列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:export')")
    @Log(title = "设备", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出设备列表")
    public void export(HttpServletResponse response, Device device)
    {
        List<Device> list = deviceService.selectDeviceList(device);
        ExcelUtil<Device> util = new ExcelUtil<Device>(Device.class);
        util.exportExcel(response, list, "设备数据");
    }

    /**
     * 新增设备
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:add')")
    @Log(title = "设备", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增设备")
    public AjaxResult add(@ApiParam(value = "设备实体", required = true) @RequestBody Device device)
    {
        return toAjax(deviceService.insertDevice(device));
    }

    /**
     * 修改设备
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:edit')")
    @Log(title = "设备", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改设备")
    public AjaxResult edit(@ApiParam(value = "设备实体", required = true) @RequestBody DeviceDto deviceDto) {
        Device device = BeanUtil.toBean(deviceDto, Device.class);
        return toAjax(deviceService.updateDevice(device));
    }

    /**
     * 删除设备
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:remove')")
    @Log(title = "设备", businessType = BusinessType.DELETE)
	@DeleteMapping("/{iotId}")
    @ApiOperation("删除设备")
    public AjaxResult remove(@ApiParam(value = "要删除的数据id的数组") @PathVariable String iotId)
    {
        System.out.println(iotId);
        //添加是否要删除的参数校验
        if (iotId == null) {
            throw new ServiceException("设备ID不能为空");
        }
        return toAjax(deviceService.deleteDeviceByIotId(iotId));
    }


    /**
     * 同步产品列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:sync')")
    @ApiOperation(value = "同步华为云平台产品")
    @PostMapping("/syncProductList")
    public R syncProductList() {
        deviceService.syncProductList();
        return R.ok();
    }

    /**
     * 查询所有产品
     */

    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/allProduct")
    @ApiOperation(value = "查询所有产品列表")
    public AjaxResult allProduct() {
        return success(deviceService.allProduct());
    }


    /**
     * 注册设备
     */
    @PostMapping("/register")
    @ApiOperation(value = "注册设备")
    public AjaxResult  registerDevice(@RequestBody DeviceDto deviceDto){
    deviceService.register(deviceDto);
    return success();
    }

    /**
     * 查询设备
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:one')")
    @ApiOperation(value = "查询设备的详细信息")
    @GetMapping("/{iotId}")
    public R<DeviceDetailVo> getDeviceInfo(@PathVariable String iotId){
        DeviceDetailVo vo = deviceService.getDeviceInfo(iotId);
        return R.ok(vo);
    }
    /**
     * 查询设备上报数据
     */
    @GetMapping("/queryServiceProperties/{iotId}")
    @ApiOperation("查询设备上报数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "iotId", value = "物联网设备id", required = true, dataTypeClass = String.class)
    })
    public AjaxResult queryServiceProperties(@PathVariable("iotId") String iotId) {
        AjaxResult ajaxResult = deviceService.queryServiceProperties(iotId);
        return ajaxResult;
    }

    /**
     * 查询产品详情
     */

    @GetMapping("/queryProduct/{productKey}")
    @ApiOperation(value = "查询产品详情")
    @ApiImplicitParams({@ApiImplicitParam(name = "productKey", value = "产品id", required = true, dataTypeClass = String.class)})
    public AjaxResult  queryProduct(@PathVariable("productKey") String productKey) {
        return deviceService.queryProduct(productKey);
    }
    }



